<?php

class Books extends CI_Controller {

    function __construct() {
        parent::__construct();

        $this->load->model('Book_model');
        $this->load->helper('form');
    }

    function index() {
        $data['page_info'] = array('title' => 'Find Books');

        $this->load->view('find-books', $data);
    }

    function search_amazon() {
        $keywords = $this->input->post('keywords');
        $item_page = $this->input->post('item_page');
        $action = $this->input->post('action');

        //Checks if search form has been submitted.
        if ($action == 'search') {

            //Calls search_amazon method
            $data['book_info'] = $this->Book_model->search_amazon($keywords, $item_page);
            $data['page_info'] = array('title' => 'Results From Amazon Search', 'search-term' => $keywords, 'item-page' => $item_page);
        } else {
            //This error message and checking could be built up a bit
            $data['page_info'] = array('title' => 'Results From Amazon Search', 'error-message' => 'Please enter in a search term.');
        }

        p($data);

        //Load the views and pass the data
        //$this->load->view('header', $data);
        //$this->load->view('find-books', $data);
    }

    function test() {
        $request = 'http://www.iblist.com/list.php?type=book&by=genre&genre=1&key=A&next=50';
        $session = curl_init($request);
        curl_setopt($session, CURLOPT_HEADER, false);
        curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
        $response = curl_exec($session);
        curl_close($session);
        
        
        $this->extrac_book_title($response);// test2($response);
        
    }
    
    function extrac_book_title($s=''){
        $p = '%<LI><A\s+HREF="(.*)"\s*>(?:.*)</A>(?:.*)<a\s+href="(.*)"\s*>(?:.*)</a></LI>%im';
        $t = preg_match_all($p, $s, $matches, PREG_PATTERN_ORDER,2);
        $a_anchor = $matches[0];
        
        
        
        p($a_anchor);
    }

    function test2($ss='') {
        $subject = '<LI><A HREF="book65871.htm">A & P (1961)</A> by <a href="author305.htm">John  Updike</a></LI>';
        $p = '%<LI><A\s+HREF="(.*)"\s*>(?:.*)</A>(?:.*)<a\s+href="(.*)"\s*>(?:.*)</a></LI>%im';
        
        $s = '  some rubbish<LI><A HREF="book65871.htm">A & P (1961)</A> by <a href="author305.htm">John  Updike</a></LI>more rubbish
                more rubbish<a name="my-anchor-name2">name2</a>more rubbish
                more rubbish<LI><a HREF="my-anchor-name3.html">name3()</a><a HREF="my-anchor-name3">name3</a></LI>more rubbish
                more rubbish<a HREF="my-anchor-name4">name4</a>more rubbish
                more rubbish<LI><A HREF="book65871.htm">A & P (1961)</A> by <a href="author305.htm">John  Updike</a></LI>more rubbish
                more rubbish<a HREF="my-anchor-name4">name4</a>more rubbish
                more rubbish<LI><a HREF="my-anchor-name4.html">name4</a><a HREF="my-anchor-name4">name4</a></LI>more rubbish
                more rubbish<a name="my-anchor-name5">name5</a>more rubbish';
        
        $t = preg_match_all($p, $ss, $matches, PREG_PATTERN_ORDER,2);
        //$t = preg_match($p, $ss, $matches);
        
        $a_anchor = $matches[0];
        
        p($matches);
        
    }
    
    function test3(){
        //$string = 'The quick brown fox jumped over the lazy dog.';
        $string = '<LI><A HREF="book65871.htm">A & P 1 (1961)</A> by <a href="author305.htm">John  Updike</a></LI>';
        $string = '%y%';//'<LI><A HREF="book65871.htm">A & P 1 (1961)</A> by <a href="author305.htm">John  Updike</a></LI>';
        $patterns = array();
        $patterns[0] = '/ab*/';
        $patterns[0] = '/^.{3}$/';
        $patterns[0] = '/[0-9]%/';
        $patterns[0] = '/%[^a-zA-Z]%/';
        //$patterns[1] = '/<\/A>by<a href=\"(.*)\">(.*)<\/a><\/LI>/';
        //$patterns[1] = '%</A>(?:.*)<a\s+href="(.*)"\s*>(?:.*)</a></LI>%im';
        //$patterns[1] = '/brown/';
        //$patterns[2] = '/fox/';
        $replacements = array();
        $replacements[0] = ' -slow- ';
        //$replacements[1] = ' -black- ';
        //$replacements[2] = 'bear';
        echo preg_replace($patterns, $replacements, $string);
    }

}
